<template>
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable class="!w-240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column prop="id" label="编号" align="center" width="80" />
      <el-table-column prop="name" label="名称" min-width="160" />
      <el-table-column prop="remark" label="描述" min-width="180" />
      <el-table-column prop="path" label="路径" min-width="220" />
      <el-table-column prop="enable" label="状态" align="center" width="120">
        <template #default="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="1"
            :inactive-value="0"
            :disabled="!checkPermi(['system:algorithm:update'])"
            @change="() => handleEnableChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" width="120" />
      <el-table-column prop="createTime" label="创建时间" :formatter="dateFormatter" width="180" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row)" v-hasPermi="['system:algorithm:update']">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['system:algorithm:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </ContentWrap>

  <AlgorithmForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { checkPermi } from '@/utils/permission'
import * as AlgorithmApi from '@/api/system/algorithm'
import AlgorithmForm from './AlgorithmForm.vue'

defineOptions({ name: 'Algorithm' })

const message = useMessage()

const loading = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const queryFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined as string | undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data: any = await AlgorithmApi.getAlgorithmPage({
      page: queryParams.pageNo,
      limit: queryParams.pageSize,
      keyWord: queryParams.name
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields?.()
  handleQuery()
}

const formRef = ref()
const openForm = (row: any) => {
  formRef.value.open(row)
}

const handleEnableChange = async (row: any) => {
  const previous = row.enable === 1 ? 0 : 1
  try {
    await AlgorithmApi.updateAlgorithm({ ...row })
    message.success('修改成功')
  } catch {
    row.enable = previous
  }
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await AlgorithmApi.deleteAlgorithm(id)
    message.success('删除成功')
    await getList()
  } catch {}
}

onMounted(() => getList())
</script>


